//采用命名空间方式
//调用方式：$.permission.leftMenu()
jQuery.permission = {
	//加载顶部菜单
	topMenu : function(){
		 $.ajax({
			url:getBasePath()+"permission/getPmsSource.do",
			type:"post",
			data:{
				parentUUID:"0",
				depth:1
			},
			cache:true,
			async:false,
			dataType:"json",
			success:function(jsonData){
				var navHtml = "";
				$.each(jsonData,function(i,pms){
					var classStr = "";
					console.log("pms.showPosition:"+pms.showPosition);
					if(pms.showPosition=='0'){
						var pmsUrl = pms.psmUrl+"?parentUUID="+pms.uuid+"&&parentTitleName="+pms.psmName;
						if(i==0){
							classStr = "class=\"selected\"";
						}
						navHtml+= "<li><a href=\""+pmsUrl+"\" target=\"leftFrame\" "+classStr+">"+
						  "<img src=\""+pms.psmIcon+"\" title=\""+pms.psmName+"\" />"+
						"<h2>"+pms.psmName+"</h2></a>"+
				       "</li>";
					}
				})
				$("#navList").html(navHtml);
			}
		})
		
		//顶部导航切换
		$(".nav li a").click(function() {
			$(".nav li a.selected").removeClass("selected")
			$(this).addClass("selected");
		})
		
		//自动点击
		setTimeout(function(){
			var url = $(".selected").attr("href");
			parent.leftFrame.document.location.href=getBasePath()+"manage/"+url;
		},20)
	},
	// 加载左边菜单，通过获取权限判断
	leftMenu : function(parentUUID) {
		$.ajax({
			url:getBasePath()+"permission/getPmsSource.do",
			type:"post",
			data:{
				parentUUID:parentUUID,
				depth:2
			},
			cache:true,
			async:false,
			dataType:"json",
			success:function(jsonData){
				var navHtml = "";
				$.each(jsonData,function(i,pms){
					var imageIndex = 0;
					var classStr = "";
					var pmsUrl = pms.psmUrl+"?parentUUID="+pms.uuid
					var startHtml = "<dd><div class=\"title\"><span><img src=\""+getBasePath()+"manage/images/leftico01.png\" /></span>"+pms.psmName+"</div>\r\n"+
				                      "<ul class=\"menuson\">\r\n";
				    var middleHtml = "";
				    $.each(pms.children,function(j,pmsChild){
				    	psmUrl = getBasePath()+pmsChild.psmUrl;
				    	if(psmUrl.indexOf("?")>-1){
				    		psmUrl+="&parentUUID="+pmsChild.uuid;
				    	}else{
				    		psmUrl+="?parentUUID="+pmsChild.uuid;
				    	}
				    	middleHtml+="<li><cite></cite><a href=\""+psmUrl+"\"  target=\"rightFrame\">"+pmsChild.psmName+"</a><i></i></li>\r\n";
				    })
					var endHtml = "</ul>\r\n</dd>";
				    navHtml+=(startHtml+middleHtml+endHtml);
				})
				$(".leftmenu").html(navHtml);
			}
		})
		
		//导航切换
		$(".menuson li").click(function(){
			$(".menuson li.active").removeClass("active")
			$(this).addClass("active");
		});
		
		$('.title').click(function(){
			var $ul = $(this).next('ul');
			$('dd').find('ul').slideUp();
			if($ul.is(':visible')){
				$(this).next('ul').slideUp();
			}else{
				$(this).next('ul').slideDown();
			}
		});
	},
	//显示权限按钮
	/*<option value="1" selected="selected">普通链接</option>
    <option value="2">增加</option>
    <option value="3">删除</option>
    <option value="4">修改</option>
    <option value="5">审批</option>
    <option value="6">恢复</option>
    <option value="7">彻底删除</option>
    <option value="8">文件导入</option>
    */
	showButton : function(parentUUID,areaId) {
		$.ajax({
			url : getBasePath() + "permission/getPmsSource.do",
			type : "post",
			data : {
				parentUUID:parentUUID,
				depth : 1
			},
			cache : true,
			async : false,
			dataType : "json",
			success : function(jsonData) {
				var navHtml = "";
				$.each(jsonData, function(i, pms) {
					if(pms.psmType==2){
						navHtml += "<a href='javascript:void(0)' id=\"addDataLink\"  class=\"linkButton\"  data-options=\"iconCls:'"
							+ pms.psmIcon + "'\">" + pms.psmName + "</a>";
					}else if(pms.psmType==3){
						navHtml += "<a href='javascript:void(0)' id=\"batchDeleteLink\"  class=\"linkButton\"  data-options=\"iconCls:'"
							+ pms.psmIcon + "'\">" + pms.psmName + "</a>";
					}else if(pms.psmType==4){
						navHtml += "<a href='javascript:void(0)' id=\"updateDataLink\"  class=\"linkButton\"  data-options=\"iconCls:'"
							+ pms.psmIcon + "'\">" + pms.psmName + "</a>";
					}else if(pms.psmType==8){
						navHtml += "<a href='javascript:void(0)' id=\"importDataLink\"  class=\"linkButton\"  data-options=\"iconCls:'"
							+ pms.psmIcon + "'\">" + pms.psmName + "</a>";
					}else{
						navHtml += "<a href=\""
							+ pms.psmUrl
							+ "\"  class=\"linkButton\"  data-options=\"iconCls:'"
							+ pms.psmIcon + "'\">" + pms.psmName + "</a>";
					}
					
				})
				$("#"+areaId).html(navHtml);
			}
		})
		$('.linkButton').linkbutton();
	},
	//显示权限按钮
	showWelcomeTools : function(parentUUID,areaId) {
		$.ajax({
			url : getBasePath() + "permission/getPmsSource.do",
			type : "post",
			data : {
				parentUUID:parentUUID,
				depth : 1
			},
			cache : true,
			async : false,
			dataType : "json",
			success : function(jsonData) {
				var navHtml = "";
				$.each(jsonData, function(i, pms) {
					navHtml += '<a href="'+pms.psmUrl+'">'+
								'<li>'+
									'<span><img src="'+getBasePath()+pms.psmIcon+'" /></span>'+
									'<p>'+pms.psmName+'</p>'+
								'</li>'+
							'</a>';
				})
				$("#"+areaId).html(navHtml);
			}
		})
	}
};  
